package com.hxkj.service.wechat.impl;

import com.hxkj.client.service.AppletArticlePushServiceClient;
import com.hxkj.client.service.AuthUserServiceClient;
import com.hxkj.domain.enums.AppletAgentArticleExceptionEnum;
import com.hxkj.domain.enums.ArticleExceptionEnum;
import com.hxkj.domain.po.*;
import com.hxkj.service.wechat.AppletArticlePushService;
import com.hxkj.token.utils.TokenUtils;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.result.ResultPageStruct;
import com.platform.common.domain.to.PageTo;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName AppletArticlePushServiceImpl
 * @Description TODO
 * @Author Mr.zheng
 * @Date 2018/6/12 18:28
 * @Version 1.0
 */
@Service
@Slf4j
public class AppletArticlePushServiceImpl implements AppletArticlePushService {

    private static final Logger LOGGER = LoggerFactory.getLogger(AppletClassifyServiceImpl.class);

    @Autowired
    private AppletArticlePushServiceClient appletArticlePushServiceClient;

    @Autowired
    private AuthUserServiceClient authUserServiceClient;

    @Autowired
    private TokenUtils tokenUtils;

    @Override
    public void saveAgentArticle(AppletAgentArticle appletAgentArticle) throws Exception {
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        try {
            if (appletAgentArticle==null){
                throw new CustomException(AppletAgentArticleExceptionEnum.AGENT_ARTICLE_NOT_SAVE);
            }
            if (null== appletAgentArticle.getArticleIds()|| appletAgentArticle.getArticleIds().equals("")){
                throw new CustomException(AppletAgentArticleExceptionEnum.AGENT_ARTICLE_NOT_ARTICLE);
            }
            if (appletAgentArticle.getAppletId()==null){
                throw new CustomException(AppletAgentArticleExceptionEnum.AGENT_ARTICLE_NOT_APPLET);
            }
            //Mr.zheng:从token中取用户ID
            AuthUser user = (AuthUser) tokenUtils.getTokenUserInfo();
            appletAgentArticle.setAgentId(user.getUserId());
            appletAgentArticle.setAgentName(user.getUserName());
            //Mr.zheng:根据token信息中的用户ID和传过来的站点ID去查询该用户的角色信息
            user.setSiteId(appletAgentArticle.getSiteId());
//            AuthRole authRole =  authUserServiceClient.getAuthRoleByUser(user).getData();
            Site site = new Site();
            site.setSiteId(appletAgentArticle.getSiteId());
            if (authUserServiceClient.getSiteById(site).getData().getSiteName().contains("嘉讯")){
                appletAgentArticle.setType(2);
                resultMessageStruct = appletArticlePushServiceClient.saveAgentArticle(appletAgentArticle);
            }else {
                appletAgentArticle.setType(1);
                resultMessageStruct = appletArticlePushServiceClient.saveAgent(appletAgentArticle);
            }

            if(resultMessageStruct.getCode()!=200){
                throw new CustomException(AppletAgentArticleExceptionEnum.getEnumByCode(resultMessageStruct.getCode()));
            }
        } catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public ResultPageStruct<AppletArticle> getHaveAgentArticle(AppletAgentArticle appletAgentArticle, PageTo pageParam) throws Exception {
        if (pageParam.getPageNum() == null || pageParam.getPageSize() == null) {
            pageParam.setPageNum(1);
            pageParam.setPageSize(10);
        }
        if (appletAgentArticle.getCreateTimeStart() == null && appletAgentArticle.getCreateTimeEnd() != null) {
            throw new CustomException(ArticleExceptionEnum.ARTICLE_CREATETIMESTRAT_NULL);
        }
        if (appletAgentArticle.getCreateTimeStart() != null && appletAgentArticle.getCreateTimeEnd() == null) {
            throw new CustomException(ArticleExceptionEnum.ARTICLE_CREATETIMESEND_NULL);
        }
        //将前端传过来的文章创建结束时间 + 一天减一秒
        if(appletAgentArticle.getCreateTimeEnd()!=null){
            appletAgentArticle.setCreateTimeEnd(appletAgentArticle.getCreateTimeEnd() + 24 * 60 * 60 * 1000 - 1);
        }
        //Mr.zheng:从token中取用户ID和站点ID
        AuthUser user = (AuthUser) tokenUtils.getTokenUserInfo();
        Site site = new Site();
        site.setSiteId(appletAgentArticle.getSiteId());
        if (authUserServiceClient.getSiteById(site).getData().getSiteName().contains("嘉讯")){
            appletAgentArticle.setAgentId(user.getUserId());
        }
        ParameterDataStruct<AppletAgentArticle> parameterDataStruct = new ParameterDataStruct<>();
        parameterDataStruct.setData(appletAgentArticle);
        parameterDataStruct.setPageTo(pageParam);
        ResultPageStruct<AppletArticle> resultPageStruct = new ResultPageStruct<>();
        try {
            resultPageStruct = appletArticlePushServiceClient.getHaveAgentArticle(parameterDataStruct);
            resultPageStruct.setCode(null);
            resultPageStruct.setMessage(null);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.info("获取文章列表失败");
        }
        return resultPageStruct;
    }

    @Override
    public ResultPageStruct<AppletArticle> getNotAgentArticle(AppletAgentArticle appletAgentArticle, PageTo pageParam) throws Exception {
        if (pageParam.getPageNum() == null || pageParam.getPageSize() == null) {
            pageParam.setPageNum(1);
            pageParam.setPageSize(10);
        }
        if (appletAgentArticle.getAppletId()==null){
            throw new CustomException(AppletAgentArticleExceptionEnum.AGENT_ARTICLE_NOT_APPLET);
        }
        if (appletAgentArticle.getCreateTimeStart() == null && appletAgentArticle.getCreateTimeEnd() != null) {
            throw new CustomException(ArticleExceptionEnum.ARTICLE_CREATETIMESTRAT_NULL);
        }
        if (appletAgentArticle.getCreateTimeStart() != null && appletAgentArticle.getCreateTimeEnd() == null) {
            throw new CustomException(ArticleExceptionEnum.ARTICLE_CREATETIMESEND_NULL);
        }
        //将前端传过来的文章创建结束时间 + 一天减一秒
        if(appletAgentArticle.getCreateTimeEnd()!=null){
            appletAgentArticle.setCreateTimeEnd(appletAgentArticle.getCreateTimeEnd() + 24 * 60 * 60 * 1000 - 1);
        }
        //Mr.zheng:从token中取用户ID和站点ID
        AuthUser user = (AuthUser) tokenUtils.getTokenUserInfo();
        Site site = new Site();
        site.setSiteId(appletAgentArticle.getSiteId());
        if (authUserServiceClient.getSiteById(site).getData().getSiteName().contains("嘉讯")){
            appletAgentArticle.setAgentId(user.getUserId());
        }
        ParameterDataStruct<AppletAgentArticle> parameterDataStruct = new ParameterDataStruct<>();
        parameterDataStruct.setData(appletAgentArticle);
        parameterDataStruct.setPageTo(pageParam);
        ResultPageStruct<AppletArticle> resultPageStruct = new ResultPageStruct<>();
        try {
            resultPageStruct = appletArticlePushServiceClient.getNotAgentArticle(parameterDataStruct);
            resultPageStruct.setCode(null);
            resultPageStruct.setMessage(null);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.info("获取文章列表失败");
        }
        return resultPageStruct;
    }


    @Override
    public ResultPageStruct<Applet> getAppletByAgentId(AppletAgent appletAgent, PageTo pageParam) throws Exception {
        if (pageParam.getPageNum() == null || pageParam.getPageSize() == null) {
            pageParam.setPageNum(1);
            pageParam.setPageSize(10);
        }
        //Mr.zheng:从token中取用户ID和站点ID
        ResultPageStruct<Applet> resultPageStruct = new ResultPageStruct<>();
        try {
            AuthUser user = (AuthUser) tokenUtils.getTokenUserInfo();
            appletAgent.setAgentId(user.getUserId());
            ParameterDataStruct<AppletAgent> parameterDataStruct = new ParameterDataStruct<>();
            parameterDataStruct.setData(appletAgent);
            parameterDataStruct.setPageTo(pageParam);
            Site site = new Site();
            site.setSiteId(appletAgent.getSiteId());
            if (!authUserServiceClient.getSiteById(site).getData().getSiteName().contains("嘉讯")){
                resultPageStruct =  appletArticlePushServiceClient.getAppletBySiteId(parameterDataStruct);
            }else {
                resultPageStruct = appletArticlePushServiceClient.getAppletByAgentId(parameterDataStruct);
            }
            resultPageStruct.setCode(null);
            resultPageStruct.setMessage(null);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.info("获取小程序列表失败");
        }
        return resultPageStruct;
    }
}
